package com.rrd.dw.mr.gzip.kafka;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.nfcs.NfcsData;
import com.ucredit.babel.common.model.nfcs.NfcsReport;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * 上海资信报告-地址&职业信息
 */
public class ShangHaiNfcsReportAddress extends ParseBase {

    private Text key = new Text();
    private Text value = new Text();

    @Override
    public boolean parseJob(Mapper.Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        NfcsReport obj = (NfcsReport) model;
        NfcsData data = obj.getData();

        StringBuilder sb = getBaseInfo(obj, successful);
        StringBuilder txSb = new StringBuilder(500);


        JSONObject info = data.getData() == null ? new JSONObject() : JSON.parseObject(data.getData());
        JSONObject personalInformation = info.getString("personalInformation") == null ? new JSONObject() : JSON.parseObject(info.getString("personalInformation"));

        JSONArray addresses = personalInformation.getString("address") == null ? new JSONArray() : (personalInformation.getJSONArray("address"));
        JSONArray workUnits = personalInformation.getString("workUnit") == null ? new JSONArray() : (personalInformation.getJSONArray("workUnit"));

        if (!addresses.isEmpty()) {
            for (int i = 0; i < addresses.size(); i++) {
                JSONObject address = addresses.getString(i) == null ? new JSONObject() : JSON.parseObject(addresses.getString(i));
                txSb.delete(0, txSb.length());
                txSb.append(address.getString("addressNo") == null ? "" : address.getString("addressNo")).append(CRT001);
                txSb.append(address.getString("addressDetail") == null ? "" : address.getString("addressDetail")).append(CRT001);
                txSb.append(CRT001);
                txSb.append(CRT001);
                txSb.append(address.getString("getDate") == null ? "" : address.getString("getDate")).append(CRT001);
                key.set(getKey());
                value.set((sb.toString() + txSb.toString()).replace("\n", " ").replace("\r", " "));
                context.write(key, value);
            }
        }

        if (!workUnits.isEmpty()) {
            for (int i = 0; i < workUnits.size(); i++) {
                JSONObject workUnit = workUnits.getString(i) == null ? new JSONObject() : JSON.parseObject(workUnits.getString(i));
                txSb.delete(0, txSb.length());
                txSb.append(workUnit.getString("workUnitNo") == null ? "" : workUnit.getString("workUnitNo")).append(CRT001);
                txSb.append(CRT001);
                txSb.append(workUnit.getString("workDetail") == null ? "" : workUnit.getString("workDetail")).append(CRT001);
                txSb.append(workUnit.getString("occupation") == null ? "" : workUnit.getString("occupation")).append(CRT001);
                txSb.append(workUnit.getString("getDate") == null ? "" : workUnit.getString("getDate")).append(CRT001);
                key.set(getKey());
                value.set((sb.toString() + txSb.toString()).replace("\n", " ").replace("\r", " "));
                context.write(key, value);
            }
        }
        return true;

    }

    @Override
    public String getTableName() {
        return "kafka_babel_nfcs_report_address_work_inc";
    }

    @Override
    public String getSubType() {
        return ".address_work";
    }

    @Override
    public String getType() {
        return RecordType.NFCS_REPORT.toString();
    }

    @Override
    public String getComment() {
        return "上海资信报告-地址&职业信息";
    }

}
